﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace MyWeb.Data
{
    public class StatisticsController : SqlDataProvider
    {
        #region [Statistics_GetByAll]

        public DataTable Statistics_GetByAll()
        {
            return GetDataTable("sp_Statistics_GetByAll");
        }

        #endregion

        #region [Statistic_Insert]

        public void Statistic_Insert(StatisticsInfo statistics)
        {
            var p2 = new SqlParameter("@User_Id", statistics.User_Id);
            var p3 = new SqlParameter("@Distribution_Id", statistics.Distribution_Id);
            var p4 = new SqlParameter("@ViewNumber", statistics.ViewNumber);
            ExecuteStoreProcedure("sp_Statistics_Insert", p2, p3, p4);
        }

        #endregion

        #region [Statistics_GetView]

        public int Statistics_GetView(int userId, int distributionId)
        {
            int viewNumber = 0;
            Con = GetCon();
            var cmd =
                new SqlCommand(
                    "select viewnumber from [Statistics] where [user_id]='" + userId + "' and Distribution_Id='" +
                    distributionId + "'", Con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                viewNumber = Int32.Parse(dr[0].ToString());
                Con.Close();
                return viewNumber;
            }
            Con.Close();
            return viewNumber;
        }

        #endregion

        #region [Statistics_UpdateView]

        public void Statistics_UpdateView(StatisticsInfo statistics)
        {
            var p1 = new SqlParameter("@User_Id", statistics.User_Id);
            var p2 = new SqlParameter("@Distribution_Id", statistics.Distribution_Id);
            var p3 = new SqlParameter("@ViewNumber", statistics.ViewNumber);
            ExecuteStoreProcedure("sp_Statistics_UpdateView", p1, p2, p3);
        }

        #endregion
    }
}